<?php
namespace app\member\model;

use app\BaseModel;
use app\commission\model\Cash;
use app\commission\model\Water;
use app\shop\model\IndexSellingProfit;
use app\shop\model\IndexSellingWater;
use think\facade\Db;

class Config extends BaseModel
{
    //会员权益表
    protected $name = 'longbing_card_v2_member_config';


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 09:49
     * @功能说明:添加
     */
    public function configAdd($data){

        $data['explain'] = !empty($data['explain'])?$data['explain']:'';

        $res = $this->insert($data);

        return $res;
    }





    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:08
     * @功能说明:详情
     */
    public function configInfo($dis){

        $data = $this->where($dis)->find();

        if(empty($data)){

           $this->configAdd($dis);

           $data = $this->where($dis)->find();

        }

        $data['growth_title']   = !empty($data['growth_title'])?$data['growth_title']:'成长值';

        $data['integral_title'] = !empty($data['integral_title'])?$data['integral_title']:'积分';

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-26 17:13
     * @功能说明:编辑
     */
    public function configUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-17 17:04
     * @功能说明:储值下单的分销
     */
    public function storeOrderCommission($order){

        $uniacid = $order['uniacid'];

        $dis = [

            'uniacid' => $uniacid
        ];

        $config = $this->configInfo($dis);

        if(empty($order['share_id'])){

            return true;
        }
        //查看是否有权限
        $auth = $this->userAuthCommission($uniacid,$order['share_id']);

        if($auth==1){

            $water_model = new IndexSellingWater();

            $cash_model  = new IndexSellingProfit();

            $pay_price   = $config['commission_price_type']==1?$config['commission_price']:round($config['commission_balance']*$order['pay_price']/100,2);

            $data = [

                'uniacid'   => $order['uniacid'],

                'source_id' => $order['user_id'],

                'user_id'   => $order['share_id'],

                'goods_id'  => $order['stored_id'],

                'type'      => 1,

                'title'     => $order['title'],

                'img'       => '',

                'price'     => $order['pay_price'],

                'extract'   => $config['commission_price_type']==1?0:$config['commission_balance'],

                'waiting'   => 2,

                'status'    => 1,

                'goods_id'  => $order['stored_id'],

                'buy_number'=> 1,

                'extract_cash' => $pay_price,

                'selling_type' => $config['commission_price_type'],

                'order_id'  => $order['id'],

                'thing_type'=> 1

            ];

            $res = $water_model->waterAdd($data);

            if($res == 1){

                $id    = $water_model->getLastInsID();

                $water = $water_model->waterInfo(['id'=>$id]);

                $cash_model->incOrDecCash($water);

            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-17 17:35
     * @功能说明:获取用户是否是不是有权限分销
     */
    public function userAuthCommission($uniacid,$user_id){

        $dis = [

            'uniacid' => $uniacid
        ];

        $config = $this->configInfo($dis);

        $auth = 0;

        if(in_array($config['commission_type'],[1,3])&&!empty($config['commission_user'])){
            //用户信息
            $user = Db::name('longbing_card_user')->where(['id'=>$user_id])->find();

            $level_model = new Level();
            //用户的等级
            $user_level  = $level_model->getUserLevel($user_id,$uniacid);

            $user_level  = !empty($user_level)?$user_level['top']:0;
            //可以获取佣金的角色
            $author = explode(',',$config['commission_user']);

            foreach ($author as &$value){
                //所有
                if($value==1000){

                    $auth = 1;
                }
                //员工 或者用户
                if(($value==888&&$user['is_staff']==1||($value==777&&$user['is_staff']==0))){

                    $auth =1;
                }
                //会员等级
                if($value==$user_level){

                    $auth =1;

                }

            }
        }

        return $auth;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-18 09:22
     * @功能说明:人工扣款佣金
     */
    public function controllerCommission($order){

        $uniacid = $order['uniacid'];

        $dis = [

            'uniacid' => $uniacid
        ];

        $config = $this->configInfo($dis);

        $top_id =  Db::name('longbing_card_user')->where(['id'=>$order['user_id']])->value('pid');

        if(in_array($config['commission_type'],[2,3])&&$config['commission_Manual_status']==1&&!empty($top_id)){

            $water_model = new IndexSellingWater();

            $cash_model  = new IndexSellingProfit();

            $pay_price   = round($config['commission_Manual_balance']*$order['pay_price']/100,2);

            $data = [

                'uniacid'   => $order['uniacid'],

                'source_id' => $order['user_id'],

                'user_id'   => $top_id,

                'goods_id'  => $order['stored_id'],

                'type'      => 1,

                'title'     => $order['title'],

                'img'       => '',

                'price'     => $order['pay_price'],

                'extract'   => $config['commission_Manual_balance'],

                'waiting'   => 2,

                'status'    => 1,

                'goods_id'  => $order['stored_id'],

                'buy_number'=> 1,

                'extract_cash' => $pay_price,

                'selling_type' => 0,

                'order_id'  => $order['id'],

                'thing_type'=> $order['controller_type']==2?2:3

            ];

            $res = $water_model->waterAdd($data);

            if($res == 1){

                $id    = $water_model->getLastInsID();

                $water = $water_model->waterInfo(['id'=>$id]);

                $cash_model->incOrDecCash($water);

            }

        }

        return true;

    }










}